package pl.register.android.database.crud;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import pl.register.android.course.Course;
import pl.register.android.database.dao.YearDao;
import pl.register.android.database.tables.YearDBTable;
import pl.register.android.year.Year;
import pl.register.android.year.YearType;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-26
 */
public class YearCRUD implements YearDao {

    private SQLiteDatabase database;

    public YearCRUD(SQLiteDatabase database) {
        this.database = database;
    }

    @Override
    public Year findYearsBy(Long yearId) {
        Year year = null;
        String where = YearDBTable.ID + " = " + yearId;
        Cursor cursor = database.query(YearDBTable.TABLE_NAME, null, where, null, null, null, null);
        if (cursor.moveToFirst()) {
            String yearTypeString = cursor.getString(cursor.getColumnIndex(YearDBTable.TYPE));
            YearType yearType = YearType.valueOf(yearTypeString);
            String yearSubName = cursor.getString(cursor.getColumnIndex(YearDBTable.SUB_NAME));
            year = new Year(yearId, yearType, yearSubName);
        }
        cursor.close();
        return year;
    }

    @Override
    public List<Year> findYearsBy(Course course) {
        List<Year> years = new ArrayList<Year>();
        String where = YearDBTable.FK_COURSE_ID + " = " + course.getId();
        Cursor cursor = database.query(YearDBTable.TABLE_NAME, null, where, null, null, null, null);
        while (cursor.moveToNext()) {
            Long yearId = cursor.getLong(cursor.getColumnIndex(YearDBTable.ID));
            String yearTypeString = cursor.getString(cursor.getColumnIndex(YearDBTable.TYPE));
            YearType yearType = YearType.valueOf(yearTypeString);
            String yearSubName = cursor.getString(cursor.getColumnIndex(YearDBTable.SUB_NAME));
            years.add(new Year(yearId, yearType, yearSubName));
        }
        cursor.close();
        return years;
    }

    @Override
    public Year insertYear(Year studentYear, Course course) {
        ContentValues insertValues = new ContentValues();
        insertValues.put(YearDBTable.TYPE, studentYear.getType().toString());
        insertValues.put(YearDBTable.SUB_NAME, studentYear.getSubName());
        insertValues.put(YearDBTable.FK_COURSE_ID, course.getId());
        long idRow = database.insert(YearDBTable.TABLE_NAME, null, insertValues);
        studentYear.setId(idRow);
        return studentYear;
    }

    @Override
    public Year updateYear(Year studentYear) {
        String where = YearDBTable.ID + " = " + studentYear.getId();
        ContentValues updateValues = new ContentValues();
        updateValues.put(YearDBTable.SUB_NAME, studentYear.getSubName());
        long res = database.update(YearDBTable.TABLE_NAME, updateValues, where, null);
        return studentYear;
    }


}
